package com.xuexi.controller;

import com.easygo.utils.MessageResults;
import com.easygo.utils.PageUtils;
import com.xuexi.api.ItemCatClient;
import com.xuexi.easygo.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Scope("prototype")
public class ItemCatController {

    @Autowired
    ItemCatClient itemCatClient;

    @RequestMapping("itemcat_page")
    public String itemcat_page(@RequestParam(value = "pageIndex",defaultValue = "1",required = false) Integer pageIndex,
                               @RequestParam(value = "pageSize",defaultValue = "5",required = false) Integer pageSize,
                               @RequestParam(value = "parent_id",defaultValue = "0",required = false) Integer parent_id,
    Model model){
        PageUtils<ItemCat> pageUtils = itemCatClient.itemcat_page(pageIndex, pageSize, parent_id);

        boolean flag= false;    //是否是三级目录
        //动态生成页面的导航条
        String str = "<li><a href=\"javascript:queryChilds(0);\" >顶级分类列表</a></li>";   //1级分类
        if(parent_id!=0){   //判断是否是一级分类，如果是0，是一级分类，没有上一级了
            //获取子级的分类
            ItemCat itemCat = itemCatClient.getItemCatById(parent_id);
            if(itemCat!=null){  //表示有2级分类
                String name1 = itemCat.getName();   //2级分类的名字
                //拼接
                String str1 ="<li><a href=\"javascript:queryChilds("+itemCat.getId()+");\" >" + name1 + "</a></li>";
                Integer parent_id1 = itemCat.getParent_id();
                ItemCat itemCat1 = itemCatClient.getItemCatById(parent_id1);
                if(itemCat1!=null){
                    String name2 = itemCat1.getName();
                    str = str + "<li><a href=\"javascript:queryChilds("+itemCat1.getId()+");\" >" + name2 + "</a></li>";
                    flag = true;    //表示已经是三级目录
                }
                str = str +str1;
            }
        }
        model.addAttribute("str",str);
        model.addAttribute("flag",flag);
        model.addAttribute("pageUtils",pageUtils);
        model.addAttribute("parent_id",parent_id);
        return "item_cat";
    }

    //添加
    @RequestMapping("addItemCat")
    @ResponseBody
    public MessageResults addItemCat(ItemCat itemCat){
        MessageResults results= null;
        int count = itemCatClient.addItemCat(itemCat);
        if (count > 0) {
            results = new MessageResults(200, "新增成功");
        } else {
            results = new MessageResults(500, "新增失败");
        }
        return results;
    }

    //更新页面的回显，所以只需要查询出来即可
    @RequestMapping("typeTemplate_goupdate")
    @ResponseBody
    public ItemCat  typeTemplate_goupdate(Integer id){
        return itemCatClient.getItemCatById(id);
    }

    @RequestMapping("updateItemCat")
    @ResponseBody
    public MessageResults updateItemCat(ItemCat itemCat){
        System.out.println("更新itemCat:"+itemCat);
        MessageResults results = null;
        //int count = itemCatClient.updateItemCat(itemCat);
        int count =1;
        if (count > 0) {
            results = new MessageResults(200, "修改成功");
        } else {
            results = new MessageResults(500, "修改失败");
        }
        return results;
    }
}
